package services;

import dao.UserInfoDao;
import utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/7
 * Time:14:57
 **/

@WebServlet("/reg")
public class RegServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //定义返回给前端的参数
        int succ = -1;//成功返回1
        String msg = "";
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //业务逻辑处理
        //注册：操作数据库添加用户
        if(username != null && !username.equals("") &&
        password != null && !password.equals("")) {
            UserInfoDao userInfoDao = new UserInfoDao();
            try {
                succ = userInfoDao.add(username, password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else{
            msg = "参数不完整！";
        }
        //返回结果
        HashMap<String,Object> ret = new HashMap<>();
        ret.put("succ",succ);
        ret.put("msg",msg);
        ResultUtils.write(response,ret);
    }
}

